// 公共函数：创建按钮
const crtButton = (type, size, plain = false, onClick) => ({
  cmpnt: "ElButton",
  events: {
    click: () => {
      onClick();
    },
  },
  props: {
    type,
    size,
    plain,
  },
});
const crtSelect = (label, model, opts, size, onChange) => ({
  cmpnt: "ElSelect",
  model,
  events: {
    "update:modelValue": (newvalue) => {
      onChange(newvalue);
    },
  },
  props: {
    label,
    size,
  },
  children: opts.map((opt) => ({
    cmpnt: "ElOption",
    props: {
      label: opt.label, // 假设每个opt对象都有label属性
      value: opt.value, // 假设每个opt对象都有value属性
      key: opt.value,
    },
  })),
});

// 公共函数：创建开关
const crtSwitch = (label, model, onUpdate) => ({
  cmpnt: "ElSwitch",
  model,
  events: {
    "update:modelValue": (newvalue) => {
      onUpdate(newvalue);
    },
  },
  props: {
    label,
  },
});
const crtText = (label, value) => ({
  cmpnt: "ElText",
  props: {
    label,
    value,
  },
});
// 公共函数：创建输入框
const crtInput = (label, placeholder, model, size, onUpdate) => ({
  cmpnt: "ElInput",
  model,
  events: {
    "update:modelValue": (newvalue) => {
      onUpdate(newvalue);
    },
  },
  props: {
    label,
    placeholder,
    size,
    clearable: true,
  },
  class: ["w-100"],
});
const crtTextArea = (label, placeholder, model, size, onUpdate) => ({
  cmpnt: "ElInput",
  model,
  events: {
    "update:modelValue": (newvalue) => {
      onUpdate(newvalue);
    },
  },
  props: {
    label,
    placeholder,
    size,
    clearable: true,
    type: "textarea",
    rows: 3,
  },
  class: ["w-100"],
});
// 公共函数：创建输入框
const crtNumInput = (label, model, size, onUpdate) => ({
  cmpnt: "ElInputNumber",
  model,
  events: {
    "update:modelValue": (newvalue) => {
      onUpdate(newvalue);
    },
  },
  props: {
    label,
    size,
  },
  class: ["w-75"],
});

const crtIcons = (label, model, size, onUpdate) => ({
  cmpnt: "ElSelect",
  model,
  events: {
    "update:modelValue": (newvalue) => {
      onChange(newvalue);
    },
  },
  props: {
    label,
    size,
  },
  templates: [
    {
      cmpnt: "template ",
      props: { "#label": "{ label, value }" },
      children: [
        {
          cmpnt: "span",
          props: "{text: '{{ label }}'",
        },
        {
          cmpnt: "span",
          props: "{text: '{{ value }}'",
        },
      ],
    },
  ],
  children: opts.map((opt) => ({
    cmpnt: "ElOption",
    props: {
      label: opt.label, // 假设每个opt对象都有label属性
      value: opt.value, // 假设每个opt对象都有value属性
      key: opt.value,
    },
  })),
});
export {
  crtButton,
  crtSelect,
  crtSwitch,
  crtText,
  crtInput,
  crtNumInput,
  crtTextArea,
  crtIcons,
};
